//
//  MLAFundsPayableVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/20.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAFundsPayableVC.h"
#import "MLAFundsPayableCell.h"
#import "MLAFundsPaybleModel.h"
#import "MLAWebVC.h"
#import "HXPayPlugin.h"


@interface MLAFundsPayableVC () <HXPayPluginDelegate>


@end

@implementation MLAFundsPayableVC {
    NSMutableArray<MLAFundsPaybleModel *> *modelsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self configTitle:@"应付款项"];
    self.tableView.rowHeight = 176;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[MLAFundsPayableCell class] forCellReuseIdentifier:kPayableCellID];
    
    self.haveRefreshHeader = YES;
//    [self loadData];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadData];
}



#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kPayableList params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self->modelsArr = [NSMutableArray array];
        self.page++;
        self.haveRefreshFooter = YES;
        self->modelsArr = [NSMutableArray array];
        self->modelsArr = [MLAFundsPaybleModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self->modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
    
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page)};
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kRunWaterList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        self.page++;
        
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self->modelsArr addObjectsFromArray:[MLAFundsPaybleModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAFundsPayableCell *cell = [tableView dequeueReusableCellWithIdentifier:kPayableCellID forIndexPath:indexPath];
    [cell.payBtn addTarget:self action:@selector(payBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    MLAFundsPaybleModel *model = modelsArr[indexPath.row];
    NSLog(@"%@",model.money);
    cell.model = model;
    return cell;
}

#pragma mark - Actions
- (void)payBtnAction:(UIButton *)sender {
    
    
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender.superview.superview.superview];
    MLAFundsPaybleModel *model = modelsArr[indexPath.row];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"支付类型" message:@"选择您要支付的类型" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"农行支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self getPayUrl:@"A" withOrderId:model.id];
    }];
    
    UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"邢台银行支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self payWithModel:model];
    }];

    
    UIAlertAction *act3 = [UIAlertAction actionWithTitle:@"其他银行支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self getPayUrl:@"6" withOrderId:model.id];
    }];
    
    UIAlertAction *act4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    [alert addAction:act1];
    [alert addAction:act2];
    [alert addAction:act3];
    [alert addAction:act4];

    [self.navigationController presentViewController:alert animated:YES completion:nil
     ];

    }

- (void)getPayUrl:(NSString *)paymentType withOrderId:(NSString *)orderId {
    
    NSDictionary *param = @{@"id":orderId,
                            @"text":@"",
                            @"type":@"2",
                            @"PaymentLinkType":@"2",
                            @"PaymentType":paymentType
                            };
    
    
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kPayAbcPay params:param success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        NSString *urlStr = response;
        MLAWebVC *webVC = [[MLAWebVC alloc] init];
        webVC.urlStr = urlStr;
        webVC.titleStr = @"支付页面";
        [self.navigationController pushViewController:webVC animated:YES];
    } failure:^{
        
    }];

}

- (void)payWithModel:(MLAFundsPaybleModel *)model {
    [HXPayPlugin startPayWithOrderSerialNumber:@"20190306137318"//流水号
                               andReqCommonURL:@"https://epay.xtbank.com/wscommon/ESB/UXUNDBF/UXUNCOMMONFRONT/common"//请求路径可不传
                              andEncryptingKey:kCryptingKey//固定加密宏
                               andEncryptingIv:kCryptingIv     /*固定加密宏*/                              andReqModeType:@"01"//固定参数
                                   andBankName:@""//银行名，可不传
                               andPayTitleName:@""//支付方式，可不传
                                 andMemberUuid:@""//设备uuid，可不传
                                  andPointRate:@"1"//传0为生产，其他为测试
                              andMemberphoneNo:@"10000000"//会员电话号码，可不传
                                  andNotifyTag:@""
                                andpayTypeCode:@"1" andTransChannel:@"012" andAppID:@"" andMemberNo:@"" andCrossMemberNo:@"" andpayViewController:self andDelegate:self];
}

- (void)HXPayPluginResult:(NSString *)resultStr {
    
}

@end
